import 'package:flutter/material.dart';
import './tabs2/DetailPage.dart';
import './tabs/TopTabPage.dart';

class TabVPage extends StatefulWidget {
  @override
  _TabVPageState createState() => _TabVPageState();
}

class _TabVPageState extends State<TabVPage> with SingleTickerProviderStateMixin {
  var _mainTabController;

  //定义底部导航项目
  final List<Tab> _bottomTabs = <Tab>[
    new Tab(text: "首页", icon: new Icon(Icons.home)),
    new Tab(text: "消息", icon: new Icon(Icons.message)),
    new Tab(text: "我的", icon: new Icon(Icons.person)),
  ];

  //定义Tab对应的页面
  final List<Widget> _tabPages = <Widget>[
    new DetailPage("首页", "开发中，敬请期待..."),
    new TopTabPage(1),
    new DetailPage("我的", "开发中，敬请期待..."),
    // new HomePage(),
    // new CustomWidget(),
    // new MyInfoPage(),
  ];

  @override
  void initState() {
    super.initState();
    _mainTabController = new TabController(
      vsync: this, //动画效果的异步处理，默认格式，背下来即可
      length: _bottomTabs.length, //需要控制的Tab页数量
    );
  }

  //当整个页面dispose时，记得把控制器也dispose掉，释放内存
  @override
  void dispose() {
    _mainTabController.dispose();
    super.dispose();
  }

  Widget build(BuildContext context) {
    return Scaffold(
      body: new TabBarView(
        controller: _mainTabController,
        // children: _buildTabPages(_bottomTabs),
        children: _tabPages,
      ),
      bottomNavigationBar: new Material(
        color: Colors.blue,
        child: new TabBar(
          controller: _mainTabController,
          tabs: _bottomTabs,
          indicatorColor: Colors.white, //tab标签的下划线颜色
        ),
      ),
    );
  }

  List<Widget> _buildTabPages(List<Tab> tabs) {
    List<Widget> pages = <Widget>[];
    for (int i = 0; i < tabs.length; i++) {
      Tab tab = tabs[i];
      Widget page = new DetailPage(tab.text, "开发中，敬请期待...");
      pages[i] = page;
    }
    return pages;
  }
}
